package com.anorak.betsy.retro;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import okhttp3.*;
import retrofit2.Call;
import retrofit2.Retrofit;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeUnit;

public class Main {
    public static void main(String[] args) throws NoSuchAlgorithmException, KeyManagementException {
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, new TrustManager[]{new CustomTrustManager()}, null);
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .sslSocketFactory(sslContext.getSocketFactory(), new CustomTrustManager()) // 设置自定义的 TrustManager
                .hostnameVerifier(new CustomHostnameVerifier()) // 设置自定义的 HostnameVerifier
                .build();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://175.178.188.169/") // 设置API的基础URL
                .client(client) // 设置自定义的OkHttpClient
                .build();

        // 创建 API 接口实例
        MyApiService apiService = retrofit.create(MyApiService.class);
        // 创建请求体
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), "your_data");
        // 发起请求
        Call<ResponseBody> call = apiService.QQINFOSTEST4();
        call.enqueue(new retrofit2.Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
                if (response.isSuccessful()) {
                    try {
                        String responseBody = response.body().string();
                        JSON json = JSONUtil.parse(responseBody);
                        System.out.println("Response: " + json);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    System.out.println("Request failed with code: " + response.code());
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable throwable) {

            }
        });
    }
}
